package com.person.leetcode.editor.cn;

/**
  * 69.x 的平方根 
  * 2022-05-10 14:46:30
**/
public class Sequence_0069_Sqrtx {
    public static void main(String[] args) {
        Solution solution = new Sequence_0069_Sqrtx().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int mySqrt(int x) {
        //整数平方根
        //x > 4 时 小于 2 即  res * res = x  res < x/2
        if (x == 0){
            return 0;
        }
         if (x <4){
            return 1;
        }else if (x == 4){
            return 2;
        }else{
            //(2,x/2.0)
            double xf = x;

             double start = 2;
             double end = xf/2;
             double middle = (end + start)/2;

            while ((int)end - (int)start > 0){
                double v = middle * middle;
                if (v == xf){
                    start = middle;
                    break;
                }
                else if (v>xf){
                    end = middle;
                }else{
                    start = middle;
                }
                middle = (end + start)/2;
            }
            return (int)start;
        }
    }
}
//leetcode submit region end(Prohibit modification and deletion)
}
